package cn.livingCloud.service.impl;

import cn.livingCloud.dto.ImageListDto;
import cn.livingCloud.dto.ImageManageListDto;
import cn.livingCloud.dto.ImageUploadConfigDto;
import cn.livingCloud.dto.ImageUploadDto;
import cn.livingCloud.entity.system.ImageResource;
import cn.livingCloud.repository.ImageResourceMapper;
import cn.livingCloud.service.ImageUploadService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by tangxu on 16-1-11.
 */
@Service
public class ImageUploadServiceImpl implements ImageUploadService {
    @Autowired
    private ImageResourceMapper mapper;

    @Override
    public ImageUploadDto saveUploadImage(ImageUploadDto imageUploadDto) {
        ImageResource resource = new ImageResource(imageUploadDto.getImageFile());
        mapper.save(resource);

        buildResult(imageUploadDto, resource);
        return imageUploadDto;
    }

    private void buildResult(ImageUploadDto imageUploadDto, ImageResource resource) {
        imageUploadDto.load(resource);
    }

    /**
     * 加载上传后的图片
     */
    @Override
    public void loadImage(HttpServletResponse response, HttpServletRequest request, String imageUrl) {
        ImageResource resource = mapper.findByUuid(imageUrl);
        String referer = request.getHeader("Referer");
        //1,图片被禁用,并且不从后台访问
        if (resource.isArchive()) {
            if (StringUtils.isEmpty(referer)) {
                return;
            }
            if (!referer.contains("admin")) {
                return;
            }
        }
        OutputStream stream = null;

        try {
            response.setContentType("image/" + resource.getExtName());
            stream = response.getOutputStream();
            stream.write(resource.getFileByte());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (stream != null) {
                    stream.flush();
                    stream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    public ImageUploadConfigDto loadImageUploadConfigDto() {
        return new ImageUploadConfigDto();
    }

    @Override
    public void loadImageListDto(ImageListDto listDto) {
        listDto.addAllList(mapper.list(listDto));
    }

    @Override
    public void list(ImageManageListDto listDto) {
        List<ImageResource> list = mapper.list(listDto);
        listDto.addAllList(list);
    }

    @Override
    public void archive(String uuid, boolean target) {
        ImageResource resource = mapper.findByUuid(uuid);
        if (resource.isArchive() == target) {
            throw new IllegalStateException("启用/禁用 失败,请检查该记录是否已经被 启用/禁用");
        }
        //如果前台的状态和数据库的状态不相同那么就执行当前传入的操作
        resource.setArchive(target);
        mapper.archive(resource);
    }
}
